﻿using System.Collections.Generic;
using Engine;
using Game;

namespace RuthlessConquest {
    public class SelectLobbyServerDialog : ListSelectionDialog {
        public const string fName = "RCSelectLobbyServerDialog";

        public class SelectLobbyServerItemWidget : CanvasWidget {
            public SelectLobbyServerItemWidget(object item) {
                if (item is not KeyValuePair<string, ServerDescription> pair) {
                    return;
                }
                Size = new Vector2(560f, 60f);
                HorizontalAlignment = WidgetAlignment.Center;
                ServerDescription desc = pair.Value;
                IsEnabled = desc != null;
                StackPanelWidget root = new() { Direction = LayoutDirection.Horizontal };
                AddChildren(root);
                root.AddChildren(
                    new CanvasWidget {
                        VerticalAlignment = WidgetAlignment.Center,
                        Size = new Vector2(120f, 60f),
                        Children = {
                            new LabelWidget {
                                Text = desc == null ? LanguageControl.Unknown : desc.Name,
                                FontScale = 0.75f,
                                Color = IsEnabled ? Color.White : Color.Gray,
                                VerticalAlignment = WidgetAlignment.Center,
                                WordWrap = true
                            }
                        }
                    }
                );
                string ip = desc?.Address.Address.ToString();
                if (desc == null
                    || pair.Key == ip) {
                    root.AddChildren(
                        new CanvasWidget {
                            VerticalAlignment = WidgetAlignment.Center,
                            Size = new Vector2(310f, 24f),
                            Margin = new Vector2(10f, 0f),
                            Children = {
                                new LabelWidget { Text = pair.Key, Color = IsEnabled ? Color.White : Color.Gray, FontScale = 0.75f, Ellipsis = true }
                            }
                        }
                    );
                }
                else {
                    StackPanelWidget panel1 = new() {
                        Direction = LayoutDirection.Vertical, VerticalAlignment = WidgetAlignment.Center, Margin = new Vector2(10f, 0f)
                    };
                    root.AddChildren(panel1);
                    panel1.AddChildren(
                        new CanvasWidget {
                            Size = new Vector2(310f, 24f),
                            Children = { new LabelWidget { Text = pair.Key, Color = Color.White, FontScale = 0.75f, Ellipsis = true } }
                        }
                    );
                    panel1.AddChildren(
                        new CanvasWidget {
                            Size = new Vector2(310f, 24f),
                            Children = { new LabelWidget { Text = ip, Color = Color.LightGray, FontScale = 0.75f, Ellipsis = true } }
                        }
                    );
                }
                StackPanelWidget panel2 = new() { Direction = LayoutDirection.Vertical, VerticalAlignment = WidgetAlignment.Center };
                root.AddChildren(panel2);
                panel2.AddChildren(
                    new LabelWidget {
                        Text = desc == null
                            ? LanguageControl.Get(fName, "2")
                            : string.Format(LanguageControl.Get(fName, "3"), (desc.Ping * 1000).ToString("0")),
                        Color = IsEnabled ? Color.White : Color.Gray,
                        FontScale = 0.75f,
                        Size = new Vector2(110f, 24f)
                    }
                );
                panel2.AddChildren(
                    new LabelWidget {
                        Text = desc == null
                            ? LanguageControl.Get(fName, "4")
                            : string.Format(LanguageControl.Get(fName, "5"), desc.GameDescriptions.Count),
                        Color = IsEnabled ? Color.White : Color.Gray,
                        FontScale = 0.75f,
                        Size = new Vector2(110f, 24f)
                    }
                );
            }
        }

        public SelectLobbyServerDialog(GameCreationParameters creationParameters) : base(
            LanguageControl.Get(fName, "1"),
            GetServersDictionary(),
            60f,
            o => new SelectLobbyServerItemWidget(o),
            o => {
                if (o is KeyValuePair<string, ServerDescription> pair
                    && pair.Value != null) {
                    DialogsManager.ShowDialog(null, new ConnectingDialog(creationParameters, ConnectingDialog.GameType.Internet, pair.Value));
                }
            }
        ) { }

        public static Dictionary<string, ServerDescription> GetServersDictionary() {
            Dictionary<string, ServerDescription> servers = new();
            foreach (ServerDescription server in ServersManager.DiscoveredServers) {
                if (ServersManager.ServerAddresses.TryGetValue(server.Address, out string name)) {
                    servers.Add(name, server);
                }
            }
            foreach (string name in SettingsManager.LobbyServers) {
                servers.TryAdd(name, null);
            }
            return servers;
        }
    }
}